package com.sundi.ecsystem.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.eccommon.dto.EcUserDto;
import com.sundi.eccommon.vo.ResultEntity;
import com.sundi.ecsystem.entity.EcUser;
import com.sundi.ecsystem.service.IEcUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/ecUser")
public class EcUserController {

    @Autowired
    IEcUserService userService;

    @RequestMapping("userList")
    public ResultEntity list(EcUserDto ecUserDto){
        PageHelper.startPage(ecUserDto.getPageNum(),ecUserDto.getPageSize());

        Wrapper<EcUser> wrapper = new EntityWrapper<EcUser>();
        wrapper.eq("deleted",1);
        if (ecUserDto.getUserName()!=null){
            wrapper.like("user_name",ecUserDto.getUserName());
        }
        List<EcUser> ecUsers = userService.selectList(wrapper);
        PageInfo<EcUser> pageInfo  = new PageInfo<>(ecUsers);
        return ResultEntity.ok(pageInfo);
    }
    @RequestMapping("addUser")
    public ResultEntity add(@RequestBody EcUserDto ecUserDto){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_name",ecUserDto.getUserName());
        EcUser user = userService.selectOne(wrapper);
        if (user!=null){
            return ResultEntity.error("您设置的用户名已存在");
        }
        EcUser ecUser = new EcUser();
        BeanUtils.copyProperties(ecUserDto,ecUser);
        ecUser.setPassword("123");
        ecUser.setDeleted(1);
        userService.insert(ecUser);
        return ResultEntity.ok("添加成功");
    }

    @RequestMapping("updUser")
    public ResultEntity upd(@RequestBody EcUserDto ecUserDto){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_name",ecUserDto.getUserName());
        EcUser user = userService.selectOne(wrapper);
        if (user!=null){
            return ResultEntity.error("您更改的用户名已存在");
        }
        EcUser ecUser = new EcUser();
        BeanUtils.copyProperties(ecUserDto,ecUser);
        userService.updateById(ecUser);
        return ResultEntity.ok("修改成功");
    }

    @RequestMapping("delUser")
    public ResultEntity del(@RequestBody EcUserDto ecUserDto){
        Wrapper<EcUser> wrapper = new EntityWrapper<EcUser>();
        wrapper.eq("id",ecUserDto.getId());
        EcUser user = new EcUser();
        BeanUtils.copyProperties(ecUserDto,user);
        user.setDeleted(0);
        userService.update(user,wrapper);
        return ResultEntity.ok("逻辑删除成功");
    }


}
